package top.jandmla.excel.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import top.jandmla.excel.bean.Menu;
import top.jandmla.excel.dao.MenusMapper;
import top.jandmla.excel.pojo.QueryPageDTO;
import top.jandmla.excel.service.MenusService;
import top.jandmla.excel.util.result.BaseResult;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class MenusServiceImpl extends ServiceImpl<MenusMapper, Menu> implements MenusService {

    @Resource
    private MenusMapper menusMapper;

    /**
     * 查询权限
     * @param dto 查询条件
     * @return 查询结果
     */
    @Override
    @Transactional(propagation= Propagation.REQUIRED,readOnly=true,rollbackFor={Exception.class})
    public IPage<Menu> selectLit(QueryPageDTO<Menu> dto) {
        Menu menu = dto.getQuery();
        QueryWrapper<Menu> qw = getQuerWrapper(menu);
        return menusMapper.selectPage(dto.getPage(),qw);
    }

    public QueryWrapper<Menu> getQuerWrapper(Menu menu){
        QueryWrapper<Menu> qw = new QueryWrapper<>();

        if (menu != null){
            if (!org.apache.commons.lang3.StringUtils.isEmpty(menu.getId())){
                qw.eq("id",menu.getId());
            }
            if (!org.apache.commons.lang3.StringUtils.isEmpty(menu.getMenuName())){
                qw.like("menu_name",menu.getMenuName());
            }
            if (!org.apache.commons.lang3.StringUtils.isEmpty(menu.getPermit())){
                qw.eq("permit",menu.getPermit());
            }
            if (!org.apache.commons.lang3.StringUtils.isEmpty(menu.getUrl())){
                qw.like("url",menu.getUrl());
            }
        }
        return qw;
    }

    /**
     * 新增权限
     * @param menu 权限信息
     * @return 新增结果
     */
    @Override
    @Transactional(propagation= Propagation.REQUIRED,readOnly=false,rollbackFor={Exception.class})
    public BaseResult<String> insertMenu(Menu menu) {
        if (menu == null || StringUtils.isEmpty(menu.getMenuName()) || StringUtils.isEmpty(menu.getUrl()) || StringUtils.isEmpty(menu.getPermit())){
            return BaseResult.fail(1,"传输参数非法！");
        }
        menu.setId(UUID.randomUUID().toString().replaceAll("-",""));
        return menusMapper.insert(menu) > 0 ? BaseResult.success("插入成功！") : BaseResult.fail(1,"插入失败，发生未知错误！");
    }

    /**
     * 查询所有权限信息
     * 注册和重新加载权限信息时使用
     * @return 权限map
     */
    @Override
    @Transactional(propagation= Propagation.REQUIRED,readOnly=true,rollbackFor={Exception.class})
    public Map<String, String> loadFilterChainDefinitions() {
        Map<String,String> map = new LinkedHashMap<>();
        map.put("/common/toLogin","anon");
        map.put("/common/login","anon");
        map.put("/common/error403","anon");
        map.put("/common/success","authc");
        map.put("/common/logout","authc");
        List<Menu> list = menusMapper.selectList(null);
        for (Menu menu : list) {
            map.put(menu.getUrl(),"perms["+menu.getPermit()+"]");
        }
        return map;
    }
}
